/**
 * logger.js
 * Copyright (C) 2007-2011 Tommi Rautava
 * 
 * This file is part of Popmungo.
 *
 * Popmungo is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Popmungo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

org.mozdev.popmungo.classes.Logger = function Logger() {};

org.mozdev.popmungo.classes.Logger.prototype = {

	/** Debug mode. 0=off, 1=on. */
	traceLevel: 1,
	
	
	/** Console Service */
	_consoleService: Components.classes['@mozilla.org/consoleservice;1'].
		getService(Components.interfaces.nsIConsoleService),
		
	/** Preference service */
	_pref: Components.classes["@mozilla.org/preferences-service;1"].
		getService(Components.interfaces.nsIPrefService).
		QueryInterface(Components.interfaces.nsIPrefBranch2),
		
	/**
	 * Send message to console.
	 * 
	 * @param message
	 */
	log: function log(message) {
		var myCaller = this.log.caller ? this.log.caller.name : "global";
		
		this._consoleService.logStringMessage(
			'Popmungo:'+ myCaller +': ' + message);
	},

	/**
	 * Send error message to console.
	 * 
	 * @param message
	 */
	logError: function logError(message) {
		var myCaller = this.logError.caller.name;

		var msg = 'Popmungo:'+ myCaller +': ' + message;

		// Is exception?				
		if (message.stack) {
 			msg = msg +':\n'+ message.stack;
		}
		
		Components.utils.reportError(
			'Popmungo:'+ myCaller +': ' + msg);

		if (this._pref.getBoolPref("extensions.popmungo.alertOnError", false)) {
			alert(myCaller +":\n"+ msg);
		}
	},
	
	
	/**
	 * Send info message to console.
	 * 
	 * @param message
	 * @see traceLevel
	 */
	debug: function debug(message) {
		if (org.mozdev.popmungo.logger.traceLevel > 0) {
			var myCaller = this.debug.caller.name;

			this._consoleService.logStringMessage(
				'Popmungo:'+ myCaller +': ' + message);
		}
	},

	
	debugObject: function debugObject(obj, objName) {
		if (org.mozdev.popmungo.logger.traceLevel > 0) {
			var myCaller = this.debugObject.caller.name;

			var message = '';
			if (obj) {
				for (var i in obj) {
					message = message + i +'='+ obj[i] +'\n';
				}
				
				message = '\n'+ message;
			} else {
				message = obj;
			}
				
			this._consoleService.logStringMessage(
				'Popmungo:'+ myCaller +': '+ (objName ? objName +'=' : '') + message);
		}
	},
	
	
	getTraceLevel:
	function getTraceLevel() {
		return this.traceLevel;
	},
	
	
	setTraceLevelFromPref:
	function setTraceLevelFromPref() {
		this.traceLevel = this._pref.getIntPref("extensions.popmungo.traceLevel");
		
		if (this.traceLevel != 0) {
			this._consoleService.logStringMessage(
					'Popmungo:setTraceLevelFromPref: traceLevel='+ this.traceLevel);
		}
	}
};

org.mozdev.popmungo.logger = new org.mozdev.popmungo.classes.Logger();

org.mozdev.popmungo.logger.setTraceLevelFromPref();
